<?php
/**
* @software         [JiJinCMS]
* @author           [上海集锦信息科技有限公司]
* @website          [http://www.jijinweb.com]
* @email            [sunny@jijinweb.com]
**/
namespace app\admin\model;
use EasyWeChat\Factory;
use EasyWeChat\Kernel\Messages\Article;
use think\facade\Session;
use think\Model;
use think\facade\Db;
class InfoModel extends Model{
    public $ModelInfo;//模型信息
    public $cat;//栏目信息
    protected $name="cms_";
    protected $suffix="cache";
    public function __construct(array $data= array()) {
        parent::__construct($data);
    }
    //保存到缓存表
    public function saveCache($data,$state){

        $cacheData=[];
        $cacheData["catid"]=$data['index']["catid"];
        $cacheData["id"]=intval($data['index']["id"]);
        $cacheData["table_name"]=$this->ModelInfo["table_name"];
        $cacheData["modelid"]=$this->ModelInfo["modelid"];
        $cacheData["title"]=$data['index']["title"];
        $cacheData["addtime"]=time();
        $cacheData["userid"]=$data['index']["userid"];
        $cacheData["username"]=$data['index']["username"];
        $cacheData["state"]=$state;
        if(input('ssssss_publish_time')){
            $cacheData["publish_time"]=strtotime(input('ssssss_publish_time'));
            $cacheData["state"]='to_publish';
        }
        $cacheData["data"]= json_encode($data);
        if($data['index']["cache_id"]){
            Db::name("cms_cache")->where("cache_id",$data['index']["cache_id"])->update($cacheData);
            $cache_id=$data['index']["cache_id"];
        }else{
            $cache_id=Db::name("cms_cache")->insertGetId($cacheData);
        }
        return $cache_id;
    }
    //审核数据（从缓存表转移到主表里,同时删除缓存表）
    public function setCheckCache($cache_id){

        $cacheData=Db::name("cms_cache")->find($cache_id);
        $data= json_decode($cacheData["data"],true);
        $data["index"]['check_userid']=Session::get('user_id');
        $data["index"]['check_username']=Session::get('username');
        $data["index"]['check_time']=time();
        //如果有id，更新
        if($data["index"]["id"]&&Db::name("cms_".$this->ModelInfo["table_name"])->find($data["index"]["id"])){
            Db::name("cms_".$this->ModelInfo["table_name"])->strict(false)->update($data["index"]);
            $data["data"]["id"]=$data["index"]["id"];
            Db::name("cms_".$this->ModelInfo["table_name"]."_data")->strict(false)->update($data["data"]);
        }else{
            //主表数据
            $id=Db::name("cms_".$this->ModelInfo["table_name"])->strict(false)->insertGetId($data["index"]);
            //副表数据
            $data["data"]["id"]=$id;
            Db::name("cms_".$this->ModelInfo["table_name"]."_data")->strict(false)->insert($data["data"]);
        }
        //删除缓存表的数据
        Db::name("cms_cache")->delete($cache_id);
        //返回主键
        return $id;
    }
    //取消审核

    //删除信息（主表删除到回收站里）
    public function DeleteIndex($ids){
        if(is_array($ids)){
            foreach($ids as $id){
                $this->IndexToCache($id);
            }
        }else{
            $this->IndexToCache($ids);
        }
    }
    //主表移到缓存表里
    public function IndexToCache($id,$state="recycle"){
        $data["index"]=$this->find($id)->toArray();
        $data["data"]=Db::name("cms_".$this->ModelInfo["table_name"]."_data")->find($id);
        $this->saveCache($data,$state);
        //删除主表和data表
        $this->where("id",$id)->delete();
        Db::name("cms_".$this->ModelInfo["table_name"]."_data")->delete($id);
    }
    //删除信息（从cache表里彻底删除）
    public function DeleteCache($ids){
        if(is_array($ids)){
            foreach($ids as $id){
                Db::name("cms_cache")->delete($id);
            }
        }else{
            Db::name("cms_cache")->delete($ids);
        }
    }
    //更新栏目信息数量
    public function updateInfoNum($catid){
        $count=$this->where(array("catid"=>$catid))->count();
        Db::name("cat")->update(array(
            "catid"=>$catid,
            "infonum"=>$count,
        ));

    }
    //整站所有相同模型的栏目（用在 信息移动，复制页面里的）
    public function selfModelCat($pid=0,$str=""){
        $where["pid"]=$pid;
        $where["status"]=1;
        $cat=Db::name("cat")->field("catid,level,name,modelid,status,sort,pid")->where($where)->order("sort asc,catid desc")->select();
        if(count($cat)>0){
            foreach($cat as $v){
                $nbsp_str="";
                for($i=1;$i<$v['level'];$i++){
                    $nbsp_str.="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                }
                if($v["modelid"]!=$this->ModelInfo['modelid']){
                    $this_selected="disabled";
                    $disabled_str="(<b>模型不同</b>)";
                }elseif($v["catid"]==$this->cat['catid']){
                    $this_selected="selected";
                    $disabled_str="";
                }else{
                    $this_selected="";
                    $disabled_str="";
                }
                $str.="<option value='{$v['catid']}' ".$this_selected.">{$nbsp_str}|-{$v['name']} $disabled_str</option>";
                $str=$this->selfModelCat($v['catid'],$str);
            }
            return $str;
        }else{
            return $str;
        }
    }
}
